package com.software.advertisecenter.service;

import com.software.advertisecenter.entity.AdvertiseInfo;
import com.software.advertisecenter.repository.AdvertiseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @Author: huanhuan
 * @Date: 2023-06-09 17:28
 **/
@Service
public class AdvertiseService {
    @Autowired
    private AdvertiseRepository advertiseRepository;

    public List<AdvertiseInfo> findAdvertiseInfosByIsUseEquals() {
        return advertiseRepository.findAdvertiseInfosByIsUseEquals("Y");
    }

    public Page<AdvertiseInfo> findAll(Integer pageNo, Integer pageSize) {
        Pageable pageable = PageRequest.of(pageNo, pageSize);
        return advertiseRepository.findAll(pageable);
    }


    public AdvertiseInfo insertAdvertiseInfo(AdvertiseInfo advertiseInfo) {
        advertiseInfo.setIsUse("Y");
        return advertiseRepository.save(advertiseInfo);
    }

    public void deleteAdvertiseInfoByAdvertiseId(String advertiseId) {
        AdvertiseInfo advertiseInfo = findById(Integer.valueOf(advertiseId));

        advertiseRepository.delete(advertiseInfo);
    }

    public void updateAdvertiseInfo(AdvertiseInfo advertiseInfo) {
        advertiseRepository.save(advertiseInfo);
    }

    public AdvertiseInfo findById(Integer id) {
        Optional<AdvertiseInfo> optional = advertiseRepository.findById(id);
        return optional.get();
    }

    public void stopAdvertiseInfoByAdvertiseId(String advertiseId) {
        AdvertiseInfo advertiseInfo = findById(Integer.valueOf(advertiseId));
        advertiseInfo.setIsUse("N");
        advertiseRepository.save(advertiseInfo);
    }

    public void startAdvertiseInfoByAdvertiseId(String advertiseId) {
        AdvertiseInfo advertiseInfo = findById(Integer.valueOf(advertiseId));
        advertiseInfo.setIsUse("Y");
        advertiseRepository.save(advertiseInfo);
    }
}
